home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / bin_d2.zoo / lisp / sort.el < prev    next >
Lisp/Scheme  |  1991-12-02  |  13KB  |  345 lines

  1. ;; Commands to sort text in an Emacs buffer.
  2. ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (provide 'sort)
  21.  
  22. ;; Original version of most of this contributed by Howie Kaye
  23.  
  24. (defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun)
  25.   "General text sorting routine to divide buffer into records and sort them.
  26. Arguments are REVERSE NEXTRECFUN ENDRECFUN &optional STARTKEYFUN ENDKEYFUN.
  27.  
  28. We consider this portion of the buffer to be divided into disjoint pieces
  29. called sort records.  A portion of each sort record (perhaps all of it)
  30. is designated as the sort key.  The records are rearranged in the buffer
  31. in order by their sort keys.  The records may or may not be contiguous.
  32.  
  33. Usually the records are rearranged in order of ascending sort key.
  34. If REVERSE is non-nil, they are rearranged in order of descending sort key.
  35.  
  36. The next four arguments are functions to be called to move point
  37. across a sort record.  They will be called many times from within sort-subr.
  38.  
  39. NEXTRECFUN is called with point at the end of the previous record.
  40. It moves point to the start of the next record.
  41. The first record is assumed to start at the position of point when sort-subr
  42. is called.
  43.  
  44. ENDRECFUN is is called with point within the record.
  45. It should move point to the end of the record.
  46.  
  47. STARTKEYFUN may moves from the start of the record to the start of the key.
  48. It may return either return a non-nil value to be used as the key, or
  49. else the key will be the substring between the values of point after
  50. STARTKEYFUN and ENDKEYFUN are called.  If STARTKEYFUN is nil, the key
  51. starts at the beginning of the record.
  52.  
  53. ENDKEYFUN moves from the start of the sort key to the end of the sort key.
  54. ENDRECFUN may be nil if STARTKEYFUN returns a value or if it would be the
  55. same as ENDRECFUN."
  56.   (save-excursion
  57.     (message "Finding sort keys...")
  58.     (let* ((sort-lists (sort-build-lists nextrecfun endrecfun
  59.                     startkeyfun endkeyfun))
  60.        (old (reverse sort-lists)))
  61.       (if (null sort-lists)
  62.       ()
  63.     (or reverse (setq sort-lists (nreverse sort-lists)))
  64.     (message "Sorting records...")
  65.     (setq sort-lists
  66.           (if (fboundp 'sortcar)
  67.           (sortcar sort-lists
  68.                (cond ((numberp (car (car sort-lists)))
  69.                   '<)
  70.                  ((consp (car (car sort-lists)))
  71.                   'buffer-substring-lessp)
  72.                  (t
  73.                   'string<)))
  74.           (sort sort-lists
  75.             (cond ((numberp (car (car sort-lists)))
  76.                    (function
  77.                 (lambda (a b)
  78.                   (< (car a) (car b)))))
  79.                   ((consp (car (car sort-lists)))
  80.                    (function
  81.                 (lambda (a b)
  82.                   (buffer-substring-lessp (car a) (car b)))))
  83.                   (t
  84.                    (function
  85.                 (lambda (a b)
  86.                   (string< (car a) (car b)))))))))
  87.     (if reverse (setq sort-lists (nreverse sort-lists)))
  88.     (message "Reordering buffer...")
  89.     (sort-reorder-buffer sort-lists old)))
  90.     (message "Reordering buffer... Done")))
  91.  
  92. ;; Parse buffer into records using the arguments as Lisp expressions;
  93. ;; return a list of records.  Each record looks like (KEY STARTPOS ENDPOS)
  94. ;; where KEY is the sort key (a number or string),
  95. ;; and STARTPOS and ENDPOS are the bounds of this record in the buffer.
  96.  
  97. ;; The records appear in the list lastmost first!
  98.  
  99. (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun)
  100.   (let ((sort-lists ())
  101.     (start-rec nil)
  102.     done key)
  103.     ;; Loop over sort records.
  104.     ;(goto-char (point-min)) -- it is the caller's responsibility to
  105.     ;arrange this if necessary
  106.     (while (not (eobp))
  107.       (setq start-rec (point))        ;save record start
  108.       (setq done nil)
  109.       ;; Get key value, or move to start of key.
  110.       (setq key (catch 'key
  111.           (or (and startkeyfun (funcall startkeyfun))
  112.               ;; If key was not returned as value,
  113.               ;; move to end of key and get key from the buffer.
  114.               (let ((start (point)))
  115.             (funcall (or endkeyfun
  116.                      (prog1 endrecfun (setq done t))))
  117.             (if (fboundp 'buffer-substring-lessp)
  118.                 (cons start (point))
  119.               (buffer-substring start (point)))))))
  120.       ;; Move to end of this record (start of next one, or end of buffer).
  121.       (cond ((prog1 done (setq done nil)))
  122.         (endrecfun (funcall endrecfun))
  123.         (nextrecfun (funcall nextrecfun) (setq done t)))
  124.       (if key (setq sort-lists (cons
  125.                  ;; consing optimization in case in which key
  126.                  ;; is same as record.
  127.                  (if (and (consp key)
  128.                       (equal (car key) start-rec)
  129.                       (equal (cdr key) (point)))
  130.                      (cons key key)
  131.                      (list key start-rec (point)))
  132.                 sort-lists)))
  133.       (and (not done) nextrecfun (funcall nextrecfun)))
  134.     sort-lists))
  135.  
  136. (defun sort-reorder-buffer (sort-lists old)
  137.   (let ((inhibit-quit t)
  138.     (last (point-min))
  139.     (min (point-min)) (max (point-max)))
  140.     (while sort-lists
  141.       (goto-char (point-max))
  142.       (insert-buffer-substring (current-buffer)
  143.                    last
  144.                    (nth 1 (car old)))
  145.       (goto-char (point-max))
  146.       (insert-buffer-substring (current-buffer)
  147.                    (nth 1 (car sort-lists))
  148.                    (nth 2 (car sort-lists)))
  149.       (setq last (nth 2 (car old))
  150.         sort-lists (cdr sort-lists)
  151.         old (cdr old)))
  152.     (goto-char (point-max))
  153.     (insert-buffer-substring (current-buffer)
  154.                  last
  155.                  max)
  156.     (delete-region min max)))    ;get rid of old version
  157.  
  158. (defun sort-lines (reverse beg end) 
  159.   "Sort lines in region alphabetically; argument means descending order.
  160. Called from a program, there are three arguments:
  161. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  162.   (interactive "P\nr")
  163.   (save-restriction
  164.     (narrow-to-region beg end)
  165.     (goto-char (point-min))
  166.     (sort-subr reverse 'forward-line 'end-of-line)))
  167.  
  168. (defun sort-paragraphs (reverse beg end)
  169.   "Sort paragraphs in region alphabetically; argument means descending order.
  170. Called from a program, there are three arguments:
  171. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  172.   (interactive "P\nr")
  173.   (save-restriction
  174.     (narrow-to-region beg end)
  175.     (goto-char (point-min))
  176.     (sort-subr reverse
  177.            (function (lambda () (skip-chars-forward "\n \t\f")))
  178.            'forward-paragraph)))
  179.  
  180. (defun sort-pages (reverse beg end)
  181.   "Sort pages in region alphabetically; argument means descending order.
  182. Called from a program, there are three arguments:
  183. REVERSE (non-nil means reverse order), BEG and END (region to sort)."
  184.   (interactive "P\nr")
  185.   (save-restriction
  186.     (narrow-to-region beg end)
  187.     (goto-char (point-min))
  188.     (sort-subr reverse
  189.            (function (lambda () (skip-chars-forward "\n")))
  190.            'forward-page)))
  191.  
  192. (defvar sort-fields-syntax-table nil)
  193. (if sort-fields-syntax-table nil
  194.   (let ((table (make-syntax-table))
  195.     (i 0))
  196.     (while (< i 256)
  197.       (modify-syntax-entry i "w" table)
  198.       (setq i (1+ i)))
  199.     (modify-syntax-entry ?\  " " table)
  200.     (modify-syntax-entry ?\t " " table)
  201.     (modify-syntax-entry ?\n " " table)
  202.     (setq sort-fields-syntax-table table)))
  203.  
  204. (defun sort-numeric-fields (field beg end)
  205.   "Sort lines in region numerically by the ARGth field of each line.
  206. Fields are separated by whitespace and numbered from 1 up.
  207. Specified field must contain a number in each line of the region.
  208. With a negative arg, sorts by the -ARG'th field, in reverse order.
  209. Called from a program, there are three arguments:
  210. FIELD, BEG and END.  BEG and END specify region to sort."
  211.   (interactive "p\nr")
  212.   (sort-fields-1 field beg end
  213.          (function (lambda ()
  214.                  (sort-skip-fields (1- field))
  215.                  (string-to-int
  216.                   (buffer-substring
  217.                     (point)
  218.                 (save-excursion
  219.                   (skip-chars-for